import {warning} from '@/utils/message'
import i18n from '@/lang'

/**
 * 设置表格样式
 * @param row
 * @returns {string}
 */
export function tableCellStyle (row) {
  // 第一列设置为左对齐
  if (row.columnIndex === 1) {
    return 'text-align:left'
  }
}

/**
 * 获取分页每页显示条数列表
 * @returns {number[]}
 */
export function getPageSizes () {
  return [15, 50, 100]
}

/**
 * 获取分页参数
 * @returns {{pageIndex: number, pageSize: number}}
 */
export function getPage () {
  return { pageIndex: 1, pageSize: 15 }
}
export function getQPage () {
  return { current: 1, size: 15 }
}
export function getQPageSizes () {
  return [15, 50, 100]
}

/**
 * 获取选择组件分页每页显示条数列表
 * @returns {number[]}
 */
export function getPickPageSizes () {
  return [10, 20, 50, 100]
}

/**
 * 获取选择组件分页参数
 * @returns {{pageIndex: number, pageSize: number}}
 */
export function getPickPage () {
  return { pageIndex: 1, pageSize: 10 }
}


/**
 * 上移操作
 * @param dataList
 * @param index
 * @return {*}
 */
export function moveUp(dataList, index) {
  if (index === 0) {
    warning(i18n.t('common.canNotMoveUp'))
    return dataList
  } else {
    let upData = dataList[index - 1]
    dataList.splice(index - 1, 1)
    dataList.splice(index, 0, upData)
    return dataList
  }
}

/**
 * 下移操作
 * @param dataList
 * @param index
 * @return {*}
 */
export function moveDown(dataList, index) {
  if (index + 1 === dataList.length) {
    warning(i18n.t('common.canNotMoveDown'))
    return dataList
  } else {
    let downData = dataList[index + 1]
    dataList.splice(index + 1, 1)
    dataList.splice(index, 0, downData)
    return dataList
  }
}


/**
 * 单选
 * @param dataList
 * @param keyName
 * @return {null|*}
 */
export function getSelectedId(dataList, keyName) {
  if (dataList.length === 0) {
    warning(i18n.t(`common.selectOne`))
    return null
  } else if (dataList.length > 1) {
    warning(i18n.t('common.onlyOne'))
    return null
  } else {
    return dataList[0][keyName]
  }
}

/**
 * 多选
 * @param dataList
 * @param keyName
 * @return {null|*}
 */
export function getSelectedIds(dataList, keyName) {
  let ids = dataList.map(item => {
    return item[keyName]
  })
  if (ids.length === 0) {
    warning(i18n.t('common.leastOne'))
    return null
  } else {
    return ids
  }
}

/**
 * 删除最后一页的所有数据时跳转到上一页
 * @param totalCount
 * @param pageSize
 * @param currentIndex
 * @param deleteSize
 * @return {number}
 */
export function getDeleteAfterPageNumber({totalCount, pageSize, currentIndex = 1, deleteSize} = {}) {
  const totalPage = Math.ceil((totalCount - deleteSize) / pageSize)
  let newPageIndex = currentIndex > totalPage ? totalPage : currentIndex
  newPageIndex = totalPage < 1 ? 1 : newPageIndex
  return newPageIndex
}
